package com.techfied.hex;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.dnd.DropTarget;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.text.html.HTML;

public class EntryPointWindow extends JFrame {

	int width = 950;
	int height = 600;

	public static HexPane file1;
	public static HexPane file2;
	public static EntryPointWindow frame;
	public static JLabel statusLabel = new JLabel("Hex Compare J : Drag and drop files to compare");
	public static int fontSize = 12;
	public EntryPointWindow(String name) {
		super(name);
		setResizable(false);
		file1 = new HexPane(new JTable(new TableModel()));
		file2 = new HexPane(new JTable(new TableModel()));
	}

	public static void main(String[] args) {
		
		frame = new EntryPointWindow("Hex Compare J");
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setLayout(new FlowLayout());
		frame.setPreferredSize(new Dimension(frame.width, frame.height));
		
		Font defaultFont = UIManager.getDefaults().getFont("TextPane.font");
		fontSize = defaultFont.getSize();
		System.out.println(fontSize);
		
		frame.getContentPane().add(file1);
		frame.getContentPane().add(new JSeparator());
		frame.getContentPane().add(file2);
		
		frame.pack();
		frame.setVisible(true);

		// status bar
		JPanel statusPanel = new JPanel();
		statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		frame.add(statusPanel, BorderLayout.SOUTH);
		statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 35));
		statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
		statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
		statusPanel.add(statusLabel);
		
		
	}

	public static void openFile(File file, HexPane hPanel) {
		
		hPanel.setFile(file);
		hPanel.clear();
		try {
			if(file1 == hPanel){
				file2.reset();
			}else if(file2 == hPanel){
				file1.reset();
			}
			
			hPanel.read(HexPane.chunksize, HexPane.APPEND);
			compare();
			hPanel.update();
			frame.toFront();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void compare() {

		for (int i = 0; i < file1.count() & i < file2.count(); i++) {
			if (file1.getData(i) != file2.getData(i)) {
				file1.highlight(i);
				file2.highlight(i);
			}
		}
	}

	public static void scroll(int direction) {
		try {
			if (direction > 0) {
				file1.moveForward();
				file2.moveForward();
			} else {
				file1.moveBackward();
				file2.moveBackward();
			}
			file1.update();
			file2.update();
			compare();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void focusCell(JTable file) {
		
	}
}
